#pragma once 

#include "InetAddress.h"
#include "TcpConnection.h"
#include "EventLoop.h"

#include <functional>
#include<list>

namespace tmms
{
    namespace network
    {
        enum
        {
            KTcpConStatusInit = 0,
            KTcpConStatusConnecting =1,
            KTcpConStatusConnected=2,
            KTcpConStatusDisConnected=3,
        };

        using ConnectionCallback = std::function<void(const TcpConnectionPtr &con,bool)>;
        class TcpClient :public TcpConnection
        {
        public:
            TcpClient(EventLoop * loop,const InetAddress &server);
            virtual ~TcpClient(); //虚的析构函数到底有啥用？

            void Connect();
          
            void SetConnectCallback(const ConnectionCallback &cb);
            void SetConnectCallback(ConnectionCallback &&cb);
         
            bool CheckError();


            //读写事件
            void OnRead()  override;
            void OnWrite() override;
            void OnClose() override;


            //
            void Send(std::list<BufferNodePtr>&list);
            void Send(const char* buf,size_t size);
            
        private:
            void ConnectInLoop();
            void UpdateConnectionStatus();
            InetAddress server_addr_;
            int32_t status_{KTcpConStatusInit};
            ConnectionCallback connected_cb_;
        };


    }

}

